package com.wzdigit.wms.wms.controller.inv.count;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.domain.inventory.LotAttrDtl;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.domain.dto.inv.count.*;
import com.wzdigit.wms.wms.service.inv.count.StockCountDtlService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@RestController
@Api(tags = "盘点任务PDA")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class StockCountDtlController {

    public static final String REQUEST_MODEL = "/stockCountDtl";

    @Autowired
    StockCountDtlService stockCountDtlService;


    @ApiOperation(value = "按物料盘点-盘点明细表头汇总",notes = "按物料盘点-盘点明细表头汇总")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/matnr/detailCount/{headerId}/{skuCode}/{isFirst}")
    public InvCountDtlCountResponse selectDetailCount(
            @PathVariable("headerId")BigDecimal headerId,
            @PathVariable("skuCode")String skuCode,
            @PathVariable("isFirst")Boolean isFirst
            , HttpServletResponse servletResponse,HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.selectDetailCount(headerId,skuCode,isFirst,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "按库位盘点-盘点明细表头汇总",notes = "按库位盘点-盘点明细表头汇总")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/stock/detailCount/{headerId}/{locCode}/{isFirst}")
    public InvCountDtlCountResponse selectDetailStockCount(
            @PathVariable("headerId")BigDecimal headerId,
            @PathVariable("locCode")String locCode,
            @PathVariable("isFirst")Boolean isFirst
            , HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.selectDetailStockCount(headerId,locCode,isFirst,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "按物料盘点明细行",notes = "按物料盘点-盘点明细行")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/matnr/detailList/{headerId}/{skuCode}/{status}/{isFirst}")
    public List<InvCountDtlResponse> selectDetailList(
            @PathVariable("headerId")BigDecimal headerId,
            @PathVariable("skuCode")String skuCode,
            @ApiParam("0 全部 1 未完成 2 有差异")
            @PathVariable("status")Integer status,
            @PathVariable("isFirst")Boolean isFirst
            , HttpServletResponse servletResponse,HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.selectDetailList(headerId,skuCode, status,isFirst);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "按库位盘点明细行",notes = "按库位盘点-盘点明细行")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/stock/detailList/{headerId}/{locCode}/{status}/{isFirst}")
    public List<InvCountDtlResponse> selectStockDetailList(
            @PathVariable("headerId")BigDecimal headerId,
            @PathVariable("locCode")String locCode,
            @ApiParam("0 全部 1 未完成 2 有差异")
            @PathVariable("status")Integer status,
            @PathVariable("isFirst")Boolean isFirst
            , HttpServletResponse servletResponse,HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.selectStockDetailList(headerId,locCode, status,isFirst);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }


    @ApiOperation(value = "获取明细行信息",notes = "获取明细行信息")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/detailInfo/{dtlId}")
    public InvCountDtlResponse detailInfo(
            @PathVariable("dtlId")BigDecimal dtlId
            , HttpServletResponse servletResponse,HttpServletRequest request){
        try {
            return stockCountDtlService.detailInfo(dtlId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }



    @ApiOperation(value = "按物料盘点 -盘点表头和明细汇总列表PDA",notes = "盘点明细列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/matnr/listDetail")
    public InvCountTotalAndDetail selectMatnrDetailList(InvCountDtlRequest detailRequest, HttpServletResponse servletResponse,HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.selectCountDtlDetailList(detailRequest,loginName,request);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "按库位盘点 -盘点表头和明细列表PDA",notes = "盘点明细主页面列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/stock/listDetail")
    public InvCountTotalAndDetail selectStockDetailList(InvCountDtlRequest detailRequest, HttpServletResponse servletResponse,HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.selectCountDtlStockList(detailRequest,loginName,request);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }





    @ApiOperation(value = "盘点提交结果",notes = "提交结果")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/submit")
    public Result submit( @RequestBody InvCountSubmitRequest submitRequest ,HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.submit(submitRequest,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "无实物盘点",notes = "无实物")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/noEntity/submit")
    public Result noEntitySubmit( @RequestBody InvCountSubmitRequest submitRequest ,Boolean isFirst, HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.noEntitysubmit(submitRequest,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }




    @ApiOperation(value = "判断物料是否有SN",notes = "判断物料是否有SN")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/hasSn/{headerId}/{skuCode}")
    public HasAndSnResponse hasSn(@PathVariable("headerId") BigDecimal headerId ,@PathVariable("skuCode") String skuCode , HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            return stockCountDtlService.hasSn(headerId,skuCode);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }






    @ApiOperation(value = "重新盘点",notes = "重新盘点")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/recount")
    public Result recount(@RequestBody List<BigDecimal>  dtlIds,HttpServletResponse servletResponse,HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.recount(dtlIds,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "新增盘盈",notes = "新增盘盈")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/addProfit")
    public Result addProfit(@RequestBody CountProfitRequest countProfitRequest, HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.addProfit(countProfitRequest,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "复盘新增盘盈获取初盘盘盈信息",notes = "复盘新增盘盈获取初盘信息")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = {REQUEST_MODEL + "/firstProfitInfo/{headerId}/{sn}",REQUEST_MODEL + "/firstProfitInfo/{headerId}"})
    public CountProfitResponse firstProfitInfo(@PathVariable("headerId")BigDecimal headerId ,@PathVariable("sn") String sn, HttpServletResponse servletResponse){
        try {
            return stockCountDtlService.firstprofitInfo(headerId,sn);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }



    @ApiOperation(value = "实盘零差异",notes = "实盘零差异")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/countNoDif/{dtlId}")
    public Result countNoDif(@PathVariable BigDecimal dtlId, HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.countNoDif(dtlId,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "录入实盘",notes = "录入实盘")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/countByWrite/{dtlId}/{qty}")
    public Result countByWrite(@PathVariable("dtlId") BigDecimal dtlId,@PathVariable("qty") BigDecimal qty, HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.countByWrite(dtlId,qty,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }


    @ApiOperation(value = "获取批次属性",notes = "获取批次属性")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/getBatchAttr/{skuId}/{headerId}")
    public List<LotAttrDtl> getBatchAttr(@PathVariable("skuId") Integer skuId, @PathVariable("headerId") BigDecimal  headerId,HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            return stockCountDtlService.getBatchAttr(skuId,headerId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }



    @ApiOperation(value = "盘点单号查询",notes = "盘点单号查询")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/invCountNoList")
    public PagerInfo<List<String>> invCountNoPageList(String invCountNo, Pager<?> pager, HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            return stockCountDtlService.invCountNoList(invCountNo,pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "盘点差异列表查询",notes = "盘点差异列表查询")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/invCountDetailDifCountPageList")
    public PagerInfo<InvCountSapDifCountResponse> invCountDetailDifCountPageList(
            @ApiParam(value = "盘点单号",required = true)
            String invCountNo,
            @ApiParam("回传状态")
            Integer status, Pager<?> pager, HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            return stockCountDtlService.invCountDetailCountPageList(invCountNo,status,pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "盘点差异删除",notes = "盘点差异删除")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/invCountDtlDelete")
    public Result invCountDtlDelete(@RequestBody List<InvCountSapDifRequest> invCountSapDifsRequest, HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            return stockCountDtlService.invCountDtlDelete(invCountSapDifsRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }




    @ApiOperation(value = "盘点差异回传SAP",notes = "盘点差异回传SAP")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/invCountDtlBackToSao/{invCountNo}")
    public Result DifBackToSap(@PathVariable("invCountNo")String invCountNo,  HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            String loginName = HttpSessionUtil.getLoginName(request);
            return stockCountDtlService.difBackToSap(invCountNo,loginName);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "根据SN查询物料编码",notes = "根据SN查询物料编码")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getSkucodeBySn/{sn}")
    public Result getSkucodeBySn(@PathVariable("sn")String sn,  HttpServletResponse servletResponse, HttpServletRequest request){
        try {
            return stockCountDtlService.getSkucodeBySn(sn);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }









}
